#define _CRT_SECURE_NO_WARNINGS 1

  struct TreeNode {
 	int val;
 	struct TreeNode *left;
 	struct TreeNode *right;
 	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  };
 
class Solution {
public:
    int lowestCommonAncestor(TreeNode* root, int o1, int o2)
    {
        if (root == nullptr) return -1;
        if (root->val == o1 || root->val == o2)
        {
            return root->val;
        }
        int left = lowestCommonAncestor(root->left, o1, o2);
        int right = lowestCommonAncestor(root->right, o1, o2);
        if (left == -1 && right == -1)
        {
            return -1;
        }
        if (left > 0 && right > 0) return root->val;
        if (left != -1) return left;
        if (right != -1) return right;
        return -1;
    }
};